package com.example.config;

import com.example.domain.Admin;
import com.example.untils.BaseContext;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class loginEmployeeIntercetor implements HandlerInterceptor {
    //目标方法执行之前 执行
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判断session是否存在empolyee
        Admin result =(Admin)request.getSession().getAttribute("admin");
        if(result==null){
            response.sendRedirect("/login.html");
            return false;
        }
        //存入TreadLocal,以便进行公共字段填充
        BaseContext.setCurrentId(result.getId());
        return true;
    }
    //在目标方法执行之后 视图对象返回之前执行
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }
    //在流程都执行完毕后  执行
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}